package com.lyp.adapters;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.RenYuanBean;
import com.lyp.jsonbean.ResetBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者:ChenXi 时间:2017/6/11 13: 04
 */

public class ResetAdapter extends RecyclerView.Adapter<ResetAdapter.MyViewHolder>{

    private List<RenYuanBean.DataBean> mList;
    private Context mContext;
    public ResetAdapter(List<RenYuanBean.DataBean> list,Context context) {
        mList = list;
        mContext = context;
    }

    @Override
    public ResetAdapter.MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        ResetAdapter.MyViewHolder holder = new ResetAdapter.MyViewHolder(LayoutInflater.from
                (viewGroup.getContext()).inflate(R.layout.item_reset, viewGroup, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ResetAdapter.MyViewHolder myViewHolder,final int i) {
        myViewHolder.mName.setText(mList.get(i).getUser());
        if (mList.get(i).getManager().equals("0")){
            myViewHolder.mManager.setText("门店人员");
        }
        else if (mList.get(i).getManager().equals("1")){
            myViewHolder.mManager.setText("门店经理");
        }
        myViewHolder.mbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    resetpwd(mList.get(i).getId());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView mName;
        TextView mManager;
        Button mbtn;

        public MyViewHolder(View view)
        {
            super(view);
            mName = (TextView) view.findViewById(R.id.tv_reset_name);
            mManager = (TextView) view.findViewById(R.id.tv_reset_zhiwei);
            mbtn = (Button) view.findViewById(R.id.button_reset);
        }
    }

    //重置密码
    public void resetpwd(String a_ids) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        YangxixiApi github = retrofit.create(YangxixiApi.class);
        Call<ResetBean> call = github.getReset(a_ids);
        call.enqueue(new Callback<ResetBean>() {
            @Override
            public void onResponse(Call<ResetBean> call, final Response<ResetBean> response) {
                if (response.body().getResult()== 1 ) {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(mContext)
                                    .content("你确定要重置密码吗？")
                                    .positiveText("确定")
                                    .negativeText("取消")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .onPositive(new MaterialDialog.SingleButtonCallback(){
                                        @Override
                                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                            new MaterialDialog.Builder(mContext)
                                                    .content("恭喜你！重置密码成功了！")
                                                    .positiveText("确定")
                                                    .canceledOnTouchOutside(false)
                                                    .cancelable(false)
                                                    .show();
                                        }
                                    })
                                    .show();
                        }
                    });
                }
            }
            @Override
            public void onFailure(Call<ResetBean> call, Throwable t) {
                ToastUtils.showShort("重置密码失败！请检查正确后再试！");
            }
        });
    }
}
